package com.vrland.web.front.dto;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import com.google.common.base.Objects;

@Entity
@Table(name = "user", uniqueConstraints = @UniqueConstraint(columnNames = { "login_name" }))
public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1642631808020502616L;

	public User() {

	}

	private User(long id, String loginName, String userName, String password,
			Role role, boolean enabled) {
		super();
		this.id = id;
		this.loginName = loginName;
		this.userName = userName;
		this.password = password;
		this.role = role;
		this.enabled = enabled;
	}

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	@Column(name = "login_name", length = 32, unique = true, nullable = false)
	private String loginName;

	@Column(name = "user_name", length = 64, nullable = false)
	private String userName;

	@Column(name = "password", length = 64, nullable = false)
	private String password;

	@Enumerated(EnumType.STRING)
	@Column(name = "role", length = 16, nullable = false)
	private Role role;

	@Column(name = "enabled", nullable = false)
	private boolean enabled;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(id, loginName, userName, password, role,
				enabled);
	}

	@Override
	public boolean equals(final Object obj) {
		if (obj instanceof User) {
			final User other = (User) obj;
			return id == other.id && Objects.equal(loginName, other.loginName)
					&& Objects.equal(userName, other.userName)
					&& Objects.equal(password, other.password)
					&& Objects.equal(role, other.role)
					&& enabled == other.enabled;
		} else {
			return false;
		}
	}

	public static User valueOf(long id, String loginName, String userName,
			String password, Role role, boolean enabled) {
		return new User(id, loginName, userName, password, role, enabled);
	}

}
